﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace LotteCinema
{
    public partial class LichChieu_DatVe : System.Web.UI.Page
    {
        public DataTable dtPhim = new DataTable();
        public DataTable dtGiaVe = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            Session["currentPage"] = "/LichChieu_DatVe.aspx";
            if (Session["matv"] == null)
                Response.Redirect("/DangKy_DangNhap.aspx");
            if (Request.QueryString["step"] != null && int.Parse(Request.QueryString["step"]) == 2)
                LoadGiaVe();
            if (Session["postback"] == null || (bool)Session["postback"] == false)
            {
                DataTable dtVe = new DataTable();
                dtVe.Columns.Add("loaighe");
                dtVe.Columns.Add("soluong");
                dtVe.Columns.Add("giave");
                dtVe.Columns.Add("tong");
                Session["VE"] = dtVe;
                Session["postback"] = true;
            }
            if (Request.QueryString["step"] != null && int.Parse(Request.QueryString["step"]) == 3)
                LoadGhe();
            if (Request["maphim"] != null)
                dtPhim = CProvider.Docbang("select * from phim where maphim='" + Request.QueryString["maphim"].ToString() + "'");
            else
                dtPhim = CProvider.Docbang("select * from phim where THOIGIANKHOICHIEU<GETDATE() and THOIGIANNGUNGCHIEU>GETDATE()");
        }

        public void LoadGiaVe()
        {
            DataTable dtVe = (DataTable)Session["VE"];
            //string madd = CProvider.LayDuLieu("select madd from phim where maphim='" + Session["maphim"].ToString() + "'");
            //string magny = CProvider.LayDuLieu("select magianiemyet from gianiemyet where madd='" + madd + "'");
            //string weekendday = Request.QueryString["weekendday"];
            //int giochieu = int.Parse(Request.QueryString["intgiochieu"]);
            //bool ngayordem = giochieu > 19;
            //string query = "select * from giave where ngayordem='" + ngayordem.ToString() + "' and magny='" + magny + "'";
            //if (weekendday == DayOfWeek.Wednesday.ToString() || weekendday == DayOfWeek.Thursday.ToString())
            //    query += " and madt='DT2'";
            //else
            //    query += " and madt='DT3'";
            //dtGiaVe = CProvider.Docbang(query);
            string giave1 = CProvider.LayDuLieu("exec SP_ThanhTien 'LG1','TV1','" + Request.QueryString["malcp"].ToString() + "','" + Request.QueryString["masc"] + "'");
            string giave2 = CProvider.LayDuLieu("exec SP_ThanhTien 'LG2','TV1','" + Request.QueryString["malcp"].ToString() + "','" + Request.QueryString["masc"] + "'");
            //for (int i = 0; i < dtGiaVe.Rows.Count; i++)
            dtVe.Rows.Add("LG1", 0, int.Parse(giave1), 0);
            dtVe.Rows.Add("LG2", 0, int.Parse(giave2), 0);
            Session["VE"] = dtVe;
            
        }

        public void LoadGhe()
        {
            Session["ghedadat"] = CProvider.Docbang("exec sp_LietKeGheDaDat '" + Request.QueryString["malcp"].ToString() + "','" + Request.QueryString["masc"].ToString() + "'");
            DataTable dtghedadat = (DataTable)Session["ghedadat"];
            DataTable vedadat = new DataTable();
            vedadat.Columns.Add("mag");
            vedadat.Columns.Add("matv");
            vedadat.Columns.Add("malcp");
            vedadat.Columns.Add("masc");
            Session["VeDaDat"] = vedadat;
            Session["maphong"] = Request.QueryString["phongchieu"];
            Session["malcp"] = Request.QueryString["malcp"];
            Session["masc"] = Request.QueryString["masc"];
            for (int i = 1; i <= 10; i++)
            {
                int khoangcach = 0;
                for (int j = 0; j < 21; j++)
                {
                    Image _ghe = new Image();
                    _ghe.AlternateText = "";
                    bool dadat = false;
                    for (int index = 0; index < dtghedadat.Rows.Count; index++)
                        if (int.Parse(dtghedadat.Rows[index][1].ToString()) == i && int.Parse(dtghedadat.Rows[index][2].ToString()) == j + 1)
                            dadat = true;
                    if (dadat)
                    {
                        _ghe.ImageUrl = "/images/seating/25/seat_1.png";
                        _ghe.Attributes.Add("id", "CGhe" + i.ToString() + (j + 1).ToString() + "_dadat");
                    }
                    else
                    {
                        _ghe.ImageUrl = "/images/seating/25/seat_0.png";
                        _ghe.Attributes.Add("id", "CGhe" + i.ToString() + (j + 1).ToString());
                        _ghe.Attributes.Add("checked", "false");
                    }
                    HyperLink _a = new HyperLink();
                    _a.Style.Add(HtmlTextWriterStyle.Position, "absolute");
                    if (j % 7 == 0)
                        khoangcach += 20;
                    _a.Style.Add(HtmlTextWriterStyle.Left, (22 * j + khoangcach).ToString() + "px");
                    _a.Style.Add(HtmlTextWriterStyle.Top, (22 * i).ToString() + "px");
                    _a.NavigateUrl = "javascript:void('')";
                    _a.Attributes.Add("class", "CGhe");
                    _a.Attributes.Add("onclick", "ChonGhe(" + i.ToString() + "," + (j + 1).ToString() + ");ChonGhe2(" + i.ToString() + "," + (j + 1).ToString() + ");");
                    _a.Controls.Add(_ghe);
                    //_a.Text = "&nbsp;";
                    panelGhe.Controls.Add(_a);
                    panelGhe.Style.Add(HtmlTextWriterStyle.Position, "relative");
                }
            }
        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dtVe = (DataTable)Session["VE"];
            dtVe.Rows[0][1] = DropDownList1.SelectedValue;
            dtVe.Rows[0][3] = int.Parse(dtVe.Rows[0][1].ToString()) * int.Parse(dtVe.Rows[0][2].ToString());
            Session["VE"] = dtVe;         
        }

        protected void DropDownList3_SelectedIndexChanged1(object sender, EventArgs e)
        {
            DataTable dtVe = (DataTable)Session["VE"];
            dtVe.Rows[1][1] = DropDownList2.SelectedValue;
            dtVe.Rows[1][3] = int.Parse(dtVe.Rows[1][1].ToString()) * int.Parse(dtVe.Rows[1][2].ToString());
            Session["VE"] = dtVe;
        }

        public int ThanhTien()
        {
            DataTable dtVe = (DataTable)Session["VE"];
            int total = 0;
            for (int i = 0; i < dtVe.Rows.Count; i++)
                total += int.Parse(dtVe.Rows[i][3].ToString());
            return total;
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Session["DirtyRead"] = false;
        }

    }
}